
//
//  UINavigationBar+BackgroundColor.m
//  tableView头部视图缩放
//
//  Created by MyMac on 15/8/10.
//  Copyright (c) 2015年 MyMac. All rights reserved.
//

#import "UINavigationBar+BackgroundColor.h"
#import <objc/runtime.h>
@implementation UINavigationBar (BackgroundColor)
static char overlayKey;
//利用rutime动态的创建属性，即在运行时创建
//动态创建属性overlay，这里是这个属性的get方法
-(UIView*)overlay
{


    return  objc_getAssociatedObject(self, &overlayKey);


}
//这是overlay这个属性的set方法
-(void)setOverlay:(UIView*)overlay
{

    objc_setAssociatedObject(self, &overlayKey, overlay, OBJC_ASSOCIATION_RETAIN_NONATOMIC);


}
//动态的创建了一个属性试图，，把这个属性试图插入到navigationBar作为背景试图
-(void)lt_setBackgroundColor:(UIColor*)backgroundColor
{
    
    if(!self.overlay){
        [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    
        self.overlay=[[UIView alloc]initWithFrame:CGRectMake(0, -20, [UIScreen mainScreen].bounds.size.width, self.bounds.size.height+20)];
         self.overlay.userInteractionEnabled = NO;
        self.overlay.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [self insertSubview:self.overlay atIndex:0];
    
    
    
    }
    self.overlay.backgroundColor=backgroundColor;



}
- (void)lt_resetl
{
    [self setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.overlay removeFromSuperview];
    self.overlay = nil;
}
@end
